package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.order.change.dispatch.mapper.ChangeOrderDispatchMapper;
import com.koron.order.change.util.ThreadLocalOrderChange;
import com.koron.order.common.bean.base.OrderType;
import com.koron.order.common.bean.po.OrderDispatch;
import com.koron.order.common.mapper.OrderDispatchMapper;
import com.koron.order.common.service.OrderDispatchService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (OrderDispatch)表服务实现类
 *
 * @author makejava
 * @since 2021-09-03 15:35:12
 */
@Service("orderDispatchService")
public class OrderDispatchServiceImpl implements OrderDispatchService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderDispatch orderDispatch) {

        if (StringUtils.isEmpty(orderDispatch.getId())) {
            orderDispatch.setId(CodeTools.getCode32());
        }
        orderDispatch.setCreateBy(getCurrUser().getAccount());
        orderDispatch.setCreateByName(getCurrUser().getName());

        int i = getOrderDispatchMapper(factory).insert(orderDispatch);
        return i;
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderDispatch orderDispatch) {
        orderDispatch.setUpdateBy(ThreadLocalContext.get().getAccount());
        orderDispatch.setUpdateByName(ThreadLocalContext.get().getName());
        int i = getOrderDispatchMapper(factory).update(orderDispatch);
        return i;
    }

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, OrderDispatch orderDispatch) {
        OrderDispatch exitsOrderDispatch = this.queryByOrderId(factory, orderDispatch.getOrderId());
        if (exitsOrderDispatch != null) {
            orderDispatch.setId(exitsOrderDispatch.getId());
        }
        return getOrderDispatchMapper(factory).insertOrUpdate(orderDispatch);

    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        return getOrderDispatchMapper(factory).deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        return getOrderDispatchMapper(factory).deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderDispatch queryById(SessionFactory factory, String id) {
        return getOrderDispatchMapper(factory).queryById(id);
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public OrderDispatch queryByOrderId(SessionFactory factory, String orderId) {

        return getOrderDispatchMapper(factory).queryByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryList")
    public PageInfo queryList(SessionFactory factory, OrderDispatch orderDispatch, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());

        List<OrderDispatch> list = getOrderDispatchMapper(factory).queryList(orderDispatch);

        return new PageInfo(list);
    }

    OrderDispatchMapper getOrderDispatchMapper(SessionFactory factory) {

        if (OrderType.CHANGE.equals(ThreadLocalOrderChange.get())) {
            return factory.getMapper(ChangeOrderDispatchMapper.class);
        }
        return factory.getMapper(OrderDispatchMapper.class);
    }


}